package com.button.springboothdfs.controller;

import com.button.springboothdfs.service.HdfsFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Description
 * @Author Administrator
 * @Date 2023/1/29 18:21
 **/
@RestController
@RequestMapping(value = "/hdfs")
@Slf4j
public class HdfsController {

    @Autowired
    private HdfsFileService hdfsFileService;

    @GetMapping("upload")
    public void upload() throws Exception {
        Path sourePath = new Path("E:\\模拟数据new.xlsx");
        Path destPath = new Path("/button", "模拟数据.xlsx");
        hdfsFileService.uploadFile(sourePath, destPath);
    }

    @GetMapping("download")
    public void download() throws IOException {
        Path filePath = new Path("/button", "模拟数据.xlsx");
        hdfsFileService.download(filePath, "E://test.xlsx");
    }

    @GetMapping("isExist")
    public Boolean isExist(@RequestParam("path") String path) throws Exception {
        Path filePath = new Path(path);
        Boolean exist = hdfsFileService.isExist(filePath);
        log.info("path={}, exist={}", path, exist);
        return exist;
    }

    @GetMapping("rename")
    public Boolean isExist() throws Exception {
        Path sourcePath = new Path("/button/模拟数据.xlsx");
        Path destPath = new Path("/button/test.xlsx");
        Boolean renameResult = hdfsFileService.rename(sourcePath, destPath);
        log.info("renameResult={}", renameResult);
        return renameResult;
    }

    @GetMapping("delete")
    public Boolean delete() throws Exception {
        Path sourcePath = new Path("/button/test.xlsx");
        Boolean delete = hdfsFileService.delete(sourcePath);
        log.info("delete={}", delete);
        return delete;
    }

    @GetMapping("getLen")
    public Long getLen(@RequestParam("path") String path) throws Exception {
        return hdfsFileService.getFileLength(path);
    }
}
